//
//  Network+Error.swift
//

import Foundation

struct AppError {
    var code: Int
    var errorMessage: String
    init(code: Int, errorMessage: String) {
        self.code = code
        self.errorMessage = errorMessage
    }
}

extension Error {
    
    var appError: AppError {
        guard let error = self as NSError? else {
            return AppError(code: 0000, errorMessage: "服务器异常")
        }
        switch error.code {
        case -999:
            return AppError(code: error.code, errorMessage: "取消网络请求")
        case -1001:
            return AppError(code: error.code, errorMessage: "网络请求超时")
        case -1200, -1201, -1202, -1203, -1205:
            return AppError(code: error.code, errorMessage: "没有配置证书")
        case -1009:
            return AppError(code: error.code, errorMessage: "网络异常，请检查您的网络后重试")
        default:
            return AppError(code: error.code, errorMessage: "服务器异常")
        }
    }
}
